/*

   注意，这个文件里不要载入非 Windows 标准的 DLL
 */


module windump.w32api;

public import std.c.windows.windows;

import windump.dll;

extern(Windows)
{

// --------------------- Kernel32.dll ---------------------------
DWORD GetModuleFileNameW(HMODULE hModule, 
        LPWSTR lpwFilename, 
        DWORD nSize);


HMODULE LoadLibraryW(LPCWSTR szPath);

static DllImport!("kernel32.dll", "IsDebuggerPresent", 
		BOOL function()) IsDebuggerPresent;


// ----------------------- user32.dll ---------------------------
int MessageBoxW(HWND, LPCWSTR szText, LPCWSTR szTitle, DWORD flags);

//-------------------- Advapi.dll -------------------------------
//

enum : DWORD
{
    POLICY_VIEW_LOCAL_INFORMATION = 0x00000001L,
    POLICY_VIEW_AUDIT_INFORMATION = 0x00000002L,
    POLICY_GET_PRIVATE_INFORMATION = 0x00000004L,
    POLICY_TRUST_ADMIN = 0x00000008L,
    POLICY_CREATE_ACCOUNT = 0x00000010L,
    POLICY_CREATE_SECRET = 0x00000020L,
    POLICY_CREATE_PRIVILEGE = 0x00000040L,
    POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080L,
    POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100L,
    POLICY_AUDIT_LOG_ADMIN = 0x00000200L,
    POLICY_SERVER_ADMIN = 0x00000400L,
    POLICY_LOOKUP_NAMES = 0x00000800L,
    POLICY_NOTIFICATION = 0x00001000L,

    POLICY_ALL_ACCESS =
        STANDARD_RIGHTS_REQUIRED |
        POLICY_VIEW_LOCAL_INFORMATION    |
        POLICY_VIEW_AUDIT_INFORMATION    |
        POLICY_GET_PRIVATE_INFORMATION   |
        POLICY_TRUST_ADMIN               |
        POLICY_CREATE_ACCOUNT            |
        POLICY_CREATE_SECRET             |
        POLICY_CREATE_PRIVILEGE          |
        POLICY_SET_DEFAULT_QUOTA_LIMITS  |
        POLICY_SET_AUDIT_REQUIREMENTS    |
        POLICY_AUDIT_LOG_ADMIN           |
        POLICY_SERVER_ADMIN              |
        POLICY_LOOKUP_NAMES,


POLICY_READ = 
    STANDARD_RIGHTS_READ             |
    POLICY_VIEW_AUDIT_INFORMATION    |
    POLICY_GET_PRIVATE_INFORMATION,

POLICY_WRITE =
    STANDARD_RIGHTS_WRITE            |    
    POLICY_TRUST_ADMIN               |                           
    POLICY_CREATE_ACCOUNT            |
    POLICY_CREATE_SECRET             |
    POLICY_CREATE_PRIVILEGE          |
    POLICY_SET_DEFAULT_QUOTA_LIMITS  |
    POLICY_SET_AUDIT_REQUIREMENTS    |
    POLICY_AUDIT_LOG_ADMIN           |
    POLICY_SERVER_ADMIN,

POLICY_EXECUTE =
    STANDARD_RIGHTS_EXECUTE          |
    POLICY_VIEW_LOCAL_INFORMATION    |
    POLICY_LOOKUP_NAMES
}

alias uint NTSTATUS;

struct CREDENTIAL_ATTRIBUTEW 
{  
    LPWSTR Keyword;  
    DWORD Flags;  
    DWORD ValueSize;  
    LPBYTE Value;
} 

alias CREDENTIAL_ATTRIBUTEW* PCREDENTIAL_ATTRIBUTEW;


struct CREDENTIALW 
{  
    DWORD Flags;  
    DWORD Type;  
    LPWSTR TargetName;  
    LPWSTR Comment;  
    FILETIME LastWritten;  
    DWORD CredentialBlobSize;  
    LPBYTE CredentialBlob;  
    DWORD Persist;  
    DWORD AttributeCount;  
    PCREDENTIAL_ATTRIBUTEW Attributes;  
    LPWSTR TargetAlias;  
    LPWSTR UserName;
} 
alias CREDENTIALW*   PCREDENTIALW;

alias WCHAR *PWSTR;

//NTSTATUS    uint;

alias DWORD ACCESS_MASK;

alias HANDLE LSA_HANDLE;
alias LSA_HANDLE*   PLSA_HANDLE;

alias void* PSID;
enum SID_NAME_USE
{
    SidTypeUser= 1,
    SidTypeGroup,
    SidTypeDomain,
    SidTypeAlias,
    SidTypeWellKnownGroup,
    SidTypeDeletedAccount,
    SidTypeInvalid,
    SidTypeUnknown,
    SidTypeComputer
} 

alias SID_NAME_USE*     PSID_NAME_USE;

struct LSA_UNICODE_STRING 
{  
    USHORT Length;  
    USHORT MaximumLength;  
    PWSTR Buffer;
} 

alias LSA_UNICODE_STRING*   PLSA_UNICODE_STRING;

struct LSA_OBJECT_ATTRIBUTES 
{  
    ULONG Length;  
    HANDLE RootDirectory;  
    PLSA_UNICODE_STRING ObjectName;  
    ULONG Attributes;  
    PVOID SecurityDescriptor;  
    PVOID SecurityQualityOfService;
} 

alias LSA_OBJECT_ATTRIBUTES*    PLSA_OBJECT_ATTRIBUTES;

// Cred APIs

// LSA APIs
static DllImport!("advapi32.dll", "LsaClose", uint function(LSA_HANDLE)) LsaClose;
static DllImport!("advapi32.dll", "LsaFreeMemory", uint function(void*)) LsaFreeMemory;

static DllImport!("advapi32.dll", "LsaOpenPolicy", 
		uint function(PLSA_UNICODE_STRING, PLSA_OBJECT_ATTRIBUTES, ACCESS_MASK, PLSA_HANDLE)
		) LsaOpenPolicy;

static DllImport!("advapi32.dll", "LsaRetrievePrivateData", 
		uint function(LSA_HANDLE, PLSA_UNICODE_STRING, PLSA_UNICODE_STRING*)
		) LsaRetrievePrivateData;

BOOL GetUserNameW(LPWSTR, LPDWORD);
BOOL GetComputerNameW(LPWSTR, LPDWORD);

} //extern(Windows)

static DllImport!("advapi32.dll", "LookupAccountNameW", 
		BOOL function(LPCWSTR, LPCWSTR, PSID, LPDWORD, LPWSTR, LPDWORD, PSID_NAME_USE)
		) LookupAccountNameW;

static DllImport!("advapi32.dll", "ConvertSidToStringSidW", 
		BOOL function(PSID,LPWSTR*)
		) ConvertSidToStringSidW;



// -------------------- OLE32.DLL -------------------------------
extern(Windows){

void CoTaskMemFree(void * pv);

} //extern(Windows)

// ------------------------ userenv.dll -----------------------

/*BOOL GetUserProfileDirectoryW(
  HANDLE hToken,
  LPWSTR lpProfileDir,
  LPDWORD lpcchSize
);*/

static DllImport!("userenv.dll", "GetProfilesDirectoryW", 
		BOOL function(LPWSTR lpProfilesDir, LPDWORD lpcchSize)) GetProfilesDirectoryW;


//-------------------------  Crypt32.dll ----------------------
extern(Windows){
struct CRYPTOAPI_BLOB 
{  
    DWORD cbData;  
    BYTE* pbData;
}
alias CRYPTOAPI_BLOB    DATA_BLOB;

struct CRYPTPROTECT_PROMPTSTRUCT 
{  
    DWORD cbSize;  
    DWORD dwPromptFlags;  
    HWND hwndApp;
    LPCWSTR szPrompt;
}
alias CRYPTPROTECT_PROMPTSTRUCT*    PCRYPTPROTECT_PROMPTSTRUCT;

enum 
{
    CRYPTPROTECT_UI_FORBIDDEN = 0x1
}

alias void* ULONG_PTR;
alias ULONG_PTR HCRYPTPROV;
typedef uint ALG_ID;
typedef ULONG_PTR HCRYPTKEY;
typedef ULONG_PTR HCRYPTHASH;

enum : uint
{
    PROV_RSA_FULL = 1,
    HP_HASHVAL = 0x0002,
    ALG_TYPE_ANY = 0,
    ALG_SID_SHA1 = 4,
    ALG_CLASS_HASH = 4 << 13,
    CALG_SHA1 = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA1
}

enum : DWORD
{
    CRYPT_STRING_BASE64HEADER =           0x00000000,
    CRYPT_STRING_BASE64 =                 0x00000001,
    CRYPT_STRING_BINARY =                 0x00000002,
    CRYPT_STRING_BASE64REQUESTHEADER =    0x00000003,
    CRYPT_STRING_HEX =                    0x00000004,
    CRYPT_STRING_HEXASCII =               0x00000005,
    CRYPT_STRING_BASE64_ANY =             0x00000006,
    CRYPT_STRING_ANY =                    0x00000007,
    CRYPT_STRING_HEX_ANY =                0x00000008,
    CRYPT_STRING_BASE64X509CRLHEADER =    0x00000009,
    CRYPT_STRING_HEXADDR =                0x0000000a,
    CRYPT_STRING_HEXASCIIADDR =           0x0000000b,
    CRYPT_STRING_NOCR =                   0x80000000
}

} //extern(Windows)

static DllImport!("crypt32.dll", "CryptUnprotectData", 
		BOOL function(DATA_BLOB* pDataIn,
			LPWSTR* ppszDataDescr,
			DATA_BLOB* pOptionalEntropy,
		    PVOID pvReserved,
		    CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct,
		    DWORD dwFlags,
		    DATA_BLOB* pDataOut)
		) CryptUnprotectData;

static DllImport!("advapi32.dll", "CryptAcquireContextW", 
		BOOL function(  HCRYPTPROV* phProv,
                        LPCWSTR pwszContainer,
            			LPCWSTR pwszProvider,
			            DWORD dwProvType,
            			DWORD dwFlags)
		) CryptAcquireContextW;

static DllImport!("advapi32.dll", "CryptCreateHash",
		BOOL function(
			HCRYPTPROV hProv,
			ALG_ID Algid,
			HCRYPTKEY hKey,
			DWORD dwFlags,
			HCRYPTHASH* phHash)
		) CryptCreateHash;

static DllImport!("advapi32.dll", "CryptHashData", 
		BOOL function(HCRYPTHASH hHash,
			BYTE* pbData,
			DWORD dwDataLen,
			DWORD dwFlags)
		) CryptHashData;

static DllImport!("advapi32.dll", "CryptGetHashParam",
		BOOL function(HCRYPTHASH hHash,
			DWORD dwParam,
			BYTE* pbData,
			DWORD* pdwDataLen,
			DWORD dwFlags)
		) CryptGetHashParam;

static DllImport!("advapi32.dll", "CryptDestroyHash", 
		BOOL function( HCRYPTHASH hHash)) CryptDestroyHash;

static DllImport!("advapi32.dll", "CryptReleaseContext",
		BOOL function(HCRYPTPROV hProv, DWORD dwFlags)
		) CryptReleaseContext;

static DllImport!("crypt32.dll", "CryptBinaryToStringW", 
		BOOL  function( BYTE* pbBinary,
			DWORD cbBinary,
			DWORD dwFlags,
			LPWSTR pwszString,
			DWORD* pcchString)
		)	CryptBinaryToStringW;
